﻿using System.ComponentModel.DataAnnotations;
using System.Linq;
using Fee.Orders;

namespace Fee
{
    public class CostDetail
    {
        /// <summary>
        ///     总投资
        /// </summary>
        public decimal TotalAmount { get; set; }

        public ConstructionType Type { get; set; }

        /// <summary>
        ///     建筑安装工程费
        /// </summary>
        public decimal ConstructionsSetupAmount { get; set; }

        /// <summary>
        ///     材料费
        /// </summary>
        public decimal MaterialFee { get; set; }

        /// <summary>
        ///     乙供材料费
        /// </summary>
        public decimal VendorMaterialFee { get; set; }

        /// <summary>
        ///     辅助材料费
        /// </summary>
        public decimal SecondaryMaterial { get; set; }

        /// <summary>
        ///     规费
        /// </summary>
        public decimal RuleCost { get; set; }

        /// <summary>
        ///     安全生产费
        /// </summary>
        public decimal SafeProduce { get; set; }

        /// <summary>
        ///     施工协调费
        /// </summary>
        public decimal ConstructionCoordination { get; set; }

        /// <summary>
        ///     建设/占道协调费
        /// </summary>
        public decimal ConstructionOccupancy { get; set; }


        /// <summary>
        ///     零星材料及资料录入费
        /// </summary>
        public decimal SporadicMaterialAndInputInfo { get; set; }

        /// <summary>
        ///     监理费（降点前）
        /// </summary>
        public decimal Supervisor { get; set; }

        /// <summary>
        ///     设计费（降点前）
        /// </summary>
        public decimal Design { get; set; }


        /// <summary>
        ///     甲供物资费用
        /// </summary>
        [Display(Name = "甲供物资费用")]
        public decimal FirstPartyMeterial { get; set; }

        /// <summary>
        ///     设备费
        /// </summary>
        public decimal Equipment { get; set; }

        /// <summary>
        ///     预覆盖数据录入费
        /// </summary>
        public decimal PreConverageDataInput { get; set; }

        /// <summary>
        ///     中间站跳纤设计费
        /// </summary>
        public decimal MiddleStationJumperDesign { get; set; }

        /// <summary>
        ///     传输资源录入费
        /// </summary>
        public decimal TransferDataInput { get; set; }

        public int Id { get; set; }

        public void CalculateBy(ConstructionTask task)
        {
            ConstructionLast = CalConstructionLast(task);
            SupervisorLast = CalSupervisorLast(task);
            DesignLast = CalSupervisorLast(task);
        }
        public decimal SupervisorLast { get; set; }
        public decimal ConstructionLast { get; set; }
        public decimal DesignLast { get; set; }

        /// <summary>
        ///     监理费（降点前）
        /// </summary>
        private decimal CalSupervisorLast(ConstructionTask task)
        {
            var taskDetail = task.Details
                .FirstOrDefault(f => f.ConstructionType == Type && f.ContractType == ContractType.Supervisor);
            if (taskDetail == null) return 0;

            return Supervisor * (100 - taskDetail.Contract.ReductionAmount) / 100;
        }

        private decimal CalConstructionLast(ConstructionTask task)
        {
            var taskDetail = task.Details
                .FirstOrDefault(f => f.ConstructionType == Type && f.ContractType == ContractType.Construction);
            if (taskDetail == null) return 0;
            //（建筑安装工程费-规费-材料费+建设/占道协调费+传输资源录入费）//×降点数
            //+规费+乙供材料费+辅助材料费+安全生产费+施工协调费+零星材料及资料录入费+预覆盖资源录入费

            var part1 = (ConstructionsSetupAmount - RuleCost - MaterialFee + ConstructionOccupancy +
                   TransferDataInput) * (100 - taskDetail.Contract.ReductionAmount) / 100;

            return part1 + RuleCost + VendorMaterialFee + SecondaryMaterial + SafeProduce +
                   SporadicMaterialAndInputInfo + PreConverageDataInput
                ;
        }

        /// <summary>
        ///     设计费（降点前）
        /// </summary>
        private decimal CalDesignLast(ConstructionTask task
        )
        {
            var taskDetail = task.Details
                .FirstOrDefault(f => f.ConstructionType == Type && f.ContractType == ContractType.Design);
            if (taskDetail == null) return 0;

            return Supervisor * (100 - taskDetail.Contract.ReductionAmount) / 100 + MiddleStationJumperDesign;
        }
    }
}